﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SPDB.BTAP.Infrastructure.Data.DapperCore
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
    /// <summary>
    /// 标识表名、主键等信息特性类
    /// </summary>
    public class TableInfoAttribute : Attribute
    {
        /// <summary>
        /// 数据库表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 主键名称
        /// </summary>
        public string PrimaryKey { get; set; }

        public TableInfoAttribute()
        { }
        public TableInfoAttribute(string tableName, string key)
        {
            this.TableName = tableName;
            this.PrimaryKey = key;
        }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class WriteAttribute : Attribute
    {
        public WriteAttribute(bool write)
        {
            Write = write;
        }
        public bool Write { get; private set; }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class DecimalFormatAttribute : Attribute
    {
        public DecimalFormatAttribute(bool isDecimal)
        {
            IsDecimal = isDecimal;
        }
        public bool IsDecimal { get; private set; }
    }

    // do not want to depend on data annotations that is not in client profile
    //[AttributeUsage(AttributeTargets.Property)]
    //public class KeyAttribute : Attribute
    //{
    //}

    public class AttributeExtend
    {
        public static bool IsWriteable(PropertyInfo pi)
        {
            object attribute = pi.GetCustomAttribute(typeof(WriteAttribute), false);
            if (attribute != null)
            {
                WriteAttribute item = (WriteAttribute)attribute;
                return item.Write;
            }
            return true;
        }

        public static bool IsDecimalFormat(PropertyInfo pi)
        {
            object attribute = pi.GetCustomAttribute(typeof(DecimalFormatAttribute), false);
            if (attribute != null)
            {
                DecimalFormatAttribute item = (DecimalFormatAttribute)attribute;
                return item.IsDecimal;
            }
            return false;
        }
    }
}